package cn.qf.shop.service.impl;

import cn.qf.shop.mapper.ActivitiesMapper;
import cn.qf.shop.pojo.entity.Activities;
import cn.qf.shop.pojo.qo.ActivitiesQO;
import cn.qf.shop.service.ActivitiesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class ActivitiesServiceImpl implements ActivitiesService {
    private Logger logger = LoggerFactory.getLogger(ActivitiesServiceImpl.class);

    @Resource
    ActivitiesMapper activitiesMapper;

    public int addAct(ActivitiesQO activitiesQO)  {
        Activities activities = new Activities();
        BeanUtils.copyProperties(activitiesQO, activities);
        int i = activitiesMapper.insertAct(activities);
        return i;
    }

    public int updateAct(ActivitiesQO activitiesQO)  {
        Activities activities = new Activities();
        BeanUtils.copyProperties(activitiesQO,activities);
        int i = activitiesMapper.updateAct(activities);
        return i;
    }

    public int deleteAct(int actId)  {
        int i = activitiesMapper.deleteAct(actId);
        return i;
    }

    public int deleteAllAct(List<Integer> actIds) {
        int i = activitiesMapper.deleteAllAct(actIds);
        return i;
    }

    public List<Activities> showAct() {
        List<Activities> activities = activitiesMapper.showAll();
        return activities;
    }

    public List<ActivitiesQO> ActivitiesList(String keyword, String createTime,String updateTime ,int limit, int offset) {
        List<Activities> activities = activitiesMapper.selectAll(keyword, createTime, updateTime, (limit-1)*offset, offset);
        if (activities.size()==0) {
            return  null;
        }else {
            List<ActivitiesQO> activitiesQOS =new ArrayList();
            for (Activities activities1:activities) {
                ActivitiesQO activitiesQO = new ActivitiesQO();
                BeanUtils.copyProperties(activities1,activitiesQO);
                activitiesQOS.add(activitiesQO);
            }
            return  activitiesQOS;
        }
    }


}
